package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.PageSearchDTO;
import com.qf.entity.SysUser;
import com.qf.mapper.UserTableMapper;
import com.qf.service.IUserTableService;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
    作者：Administrator
    时间：2021/9/27 0027 10:25
* */
@Service
public class UserTableService implements IUserTableService {
    @Autowired
    private UserTableMapper userTableMapper;

    @Override
    public PageResult<SysUser> selectByPage(PageSearchDTO<SysUser> pageSearchDTO) {
        if (pageSearchDTO == null) {
            return  new PageResult<>(false, "参数不合法");
        }

        //设置分页信息
        PageHelper.startPage(pageSearchDTO.getPage(), pageSearchDTO.getSize());

        List<SysUser> byPage = userTableMapper.findByPage( pageSearchDTO.getWhere());

        //获取分页信息
        PageInfo<SysUser> pageInfo = new PageInfo<>(byPage);

        PageResult<SysUser> pageResult = new PageResult<>(true,"success");

        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());
        return pageResult;
    }

    @Override
    public Result save(SysUser sysUser) {

        if (sysUser == null && sysUser.getUserId() == null) {
            return  new Result(false, "参数不合法");
        }

        sysUser.setEnabled("0");

        int save = userTableMapper.save(sysUser);

        return new Result(save==1?true:false, save==1?"success":"fail");
    }
}
